package com.changan.controller;

import com.changan.response.Result;
import com.changan.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author NieChangan
 */
@Api
@RestController
@CrossOrigin
public class AliOssController {
    @Autowired
    private OssService ossService;

    @ApiOperation(value = "列举文件")
    @PostMapping("/list")
    public Result list(MultipartFile file){
        ossService.listFile();
        return Result.ok();
    }

    @ApiOperation(value = "上传图片文件")
    @PostMapping("/uploadImgFile")
    public Result uploadImgFile(MultipartFile file){
        String s = ossService.upload(file);
        return Result.ok().data("url",s);
    }

    @ApiOperation(value = "删除上传替换之后的头像")
    @PostMapping("/deleteImgFile")
    public Result deleteImgFile(String file){
        try {
            String[] splitFile = file.split(".com/");
            ossService.deleteFile(splitFile[1]);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }
}
